# coding=utf-8
'''
Created on 2012-8-26

@author: quanwei
'''
from Queue import Queue
from core import Worker,HttpWorker

class Dispatcher(object):
    workerNum = 2
    workers = {}
    taskQueue = Queue()
    workerClassName = "Worker"
    def __init__(self,workerNum,workerName):
        self.workerNum = workerNum
        self.workerClassName = workerName
    
    def getTaskQueue(self):
        return self.taskQueue
    
    def start(self):        
        for i in range(0,self.workerNum):
            code  = self.workerClassName + "." + self.workerClassName + "('worker_" + str(i) + "',self)"
            self.workers[i] = eval(code)
            self.workers[i].start()
        for i in range(0,self.workerNum):
            self.workers[i].join()
    
    def addTask(self,task):
        self.taskQueue.put(task, True, None)
    
    def stop(self):
        for i in range(0,self.workerNum):
            self.workers[i].run = False